//
//  main.cpp
//  experiment_week_homework
//
//  Created by ClownQiang on 14-5-27.
//  Copyright (c) 2014年 MMClub. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	char str[100];
	char search_letter[50];
	char letter[63];
	int count[63]={0};
	int i;
	int lc=0;
    
	ifstream fin("/Users/clownqiang/XCodeProject/experiment_week_homework/experiment_week_homework/data.txt");
	if(!fin)
    {
        cerr<<"open file error!"<<endl;
        exit(1);
    }
    
	for(i=0;i<26;i++)
		letter[i]='A'+i;
	for(i=26;i<52;i++)
		letter[i]='a'+i-26;
	for(i=52;i<62;i++)
		letter[i]='0'+i-52;
	letter[62]=32;
    
	while(fin.getline(str,100)){
        i=0;
        while(str[i]!='\0')
        {
            if(str[i]>='A'&&str[i]<='Z')
            {
                count[str[i]-65]++;
                lc++;
            }
            if(str[i]>='a'&&str[i]<='z')
            {
                count[str[i]-97+26]++;
                lc++;
            }
            if (str[i]>='0'&&str[i]<='9')
            {
                count[str[i]-48+52]++;
                lc++;
            }
            if (str[i]==' ')
            {
                count[62]++;
                lc++;
            }
            i++;
        }
    }
    
	cout<<"|----------|------------|---------------|"<<endl;
	cout<<"|   字母   |  出现次数  |   出现频率    |"<<endl;
	cout<<"|----------|------------|---------------|"<<endl;
	for(i=0;i<63;i++)
	{
		if(count[i]!=0){
			if (letter[i]==' '){
				cout<<"| "<<"space"<<"    |\t"<<count[i]<<"\t|  "<<(float)count[i]/lc*100<<"\t|"<<endl;
				cout<<"|----------|------------|---------------|"<<endl;
			}
			else{
				cout<<"|    "<<letter[i]<<"     |\t"<<count[i]<<"\t|  "<<(float)count[i]/lc*100<<"\t|"<<endl;
				cout<<"|----------|------------|---------------|"<<endl;
            }
        }
	}
    
	cout<<"输入单词或字母查找："<<endl;
	cin.getline(search_letter,100);
	if (search_letter[1]=='\0')
	{
		if (count[search_letter[0]-65]==0||count[search_letter[0]-97+26])
		{
			
			if(search_letter[0]>='A'&&search_letter[0]<='Z')
			{
				cout<<"查找到字母："<<search_letter<<"，一共有："<<count[search_letter[0]-65]<<"个"<<endl;
			}else if(search_letter[0]>='a'&&search_letter[0]<='z'){
				cout<<"查找到字母："<<search_letter<<"，一共有："<<count[search_letter[0]-97+26]<<"个"<<endl;
			}
		}else {
			cout<<"抱歉没有查到"<<endl;
		}
	}else {
		int word_num = 0;
		ifstream sfin("data.txt");
		while(sfin.getline(str,100))                 //读入一行至str
    	{
           	istringstream stream(str);     //用来从独到的一行内容中逐个取出单词
        	string word;                          //记录每行中的单独的每个单词
       		while (stream >> word)     //取出每行中每个单词
       		{
            	if (search_letter == word)  //是否为待查单词
                {
                    word_num++;
                }
        	}
        }
        if(word_num == 0){
            cout<<"抱歉没有查到单词"<<endl;
        }else{
            cout<<"查找到单词："<<search_letter<<"，一共有："<<word_num<<"个"<<endl;	
        }
	}
	return 0;
}
